package com.fjut.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjut.mapper.CourseMapper;
import com.fjut.model.vod.Course;
import com.fjut.model.vod.CourseDescription;
import com.fjut.model.vod.Subject;
import com.fjut.model.vod.Teacher;
import com.fjut.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjut.vo.vod.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fjut.dateBase.CourseField.*;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author DengTianxun
 * @since 2023-03-06
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements CourseService {

    private static final String RECORDS = "records";
    private static final String TOTAL = "totalCount";
    private static final String TOTAL_PAGE = "totalPage";
    private static final String PAGES = "pages";
    private static final String TEACHER_NAME = "teacherName";
    private static final String SUBJECT_PARENT_TITLE = "subjectParentTitle";
    private static final String SUBJECT_TITLE = "subjectTitle";
    private static final Integer PUBLISH_STATUS = 1;
    private static final Integer UN_PUBLISH_STATUS = 0;

    private static final String COURSE_VO = "courseVo";
    private static final String CHAPTER_VO_LIST = "chapterVoList";
    private static final String DESCRIPTION = "description";
    private static final String TEACHER = "teacher";
    private static final String IS_BUY = "isBuy";

    @Autowired
    private TeacherService teacherService;
    @Autowired
    private SubjectService subjectService;
    @Autowired
    private CourseDescriptionService courseDescriptionService;
    @Autowired
    private VideoService videoService;
    @Autowired
    private ChapterService chapterService;

    @Override
    public Map<String,Object> courseList(Page<Course> coursePage, CourseQueryVo courseQueryVo) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(courseQueryVo.getTitle())) {
            wrapper.like(TITLE,courseQueryVo.getTitle());
        }
        if (!StringUtils.isEmpty(courseQueryVo.getTeacherId())) {
            wrapper.eq(TEACHER_ID,courseQueryVo.getTeacherId());
        }
        if (!StringUtils.isEmpty(courseQueryVo.getSubjectId())) {
            wrapper.eq(SUBJECT_ID,courseQueryVo.getSubjectId());
        }
        if (!StringUtils.isEmpty(courseQueryVo.getSubjectParentId())) {
            wrapper.eq(SUBJECT_PARENT_ID,courseQueryVo.getSubjectParentId());
        }
        baseMapper.selectPage(coursePage,wrapper);
        List<Course> records = coursePage.getRecords();
        records.stream().forEach(record -> {
            this.getTeacherAndSubjectName(record);
        });
        Map<String,Object> map = new HashMap<>();
        map.put(RECORDS,records);
        map.put(TOTAL,coursePage.getTotal());
        map.put(PAGES,coursePage.getPages());
        return map;
    }

    @Override
    public Long saveInfo(CourseFormVo courseFormVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.insert(course);
        CourseDescription courseDescription = new CourseDescription();
        courseDescription.setId(course.getId());
        courseDescription.setDescription(courseFormVo.getDescription());
        courseDescriptionService.save(courseDescription);
        return course.getId();
    }

    @Override
    public Boolean deleteCourse(Long id) {
        videoService.removeVideoByCourseId(id); // 删除小节
        chapterService.removeChapterByCourseId(id); // 删除章节
        courseDescriptionService.removeCourseDescriptionByCourseId(id); // 删除课程简介
        int delete = baseMapper.deleteById(id);// 删除课程
        return delete > 0 ? true:false;
    }

    @Override
    public CourseFormVo getCourseInfoById(Long id) {
        Course course = baseMapper.selectById(id);
        if (course == null) {
            return null;
        }
        CourseDescription courseDescription = courseDescriptionService.getById(course.getId());
        CourseFormVo courseFormVo = new CourseFormVo();
        BeanUtils.copyProperties(course,courseFormVo);
        if (courseDescription != null) {
            courseFormVo.setDescription(courseDescription.getDescription());
        }
        return courseFormVo;
    }

    @Override
    public void updateCourseInfo(CourseFormVo courseFormVo) {
        Course course = new Course();
        BeanUtils.copyProperties(courseFormVo,course);
        baseMapper.updateById(course);
        CourseDescription courseDescription = courseDescriptionService.getById(courseFormVo.getId());
        if (courseDescription != null) {
            courseDescription.setDescription(courseFormVo.getDescription());
            courseDescriptionService.updateById(courseDescription);
        }
    }

    @Override
    public CoursePublishVo getCoursePublishVo(Long id) {
        return baseMapper.selectGetCoursePublishVoById(id);
    }

    @Override
    public Boolean publishCourse(Long id) {
        Course course = baseMapper.selectById(id);
        if (course == null) {
            return false;
        }
        course.setStatus(PUBLISH_STATUS);
        course.setPublishTime(new Date());
        baseMapper.updateById(course);
        return true;
    }

    @Override
    public List<Course> getCourseLike(String keyWord) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        if (StringUtils.isEmpty(keyWord)) {
            return null;
        }
        wrapper.like(TITLE,keyWord);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public Map<String,Object> getCourseListByType(CourseQueryVo courseQueryVo, Page<Course> coursePage) {
        QueryWrapper<Course> wrapper = new QueryWrapper<>();
        Long teacherId = courseQueryVo.getTeacherId();
        Long subjectId = courseQueryVo.getSubjectId();
        Long subjectParentId = courseQueryVo.getSubjectParentId();
        String title = courseQueryVo.getTitle();
        if (!StringUtils.isEmpty(teacherId)) {
            wrapper.eq(TEACHER_ID,teacherId);
        }
        if (!StringUtils.isEmpty(subjectId)) {
            wrapper.eq(SUBJECT_ID,subjectId);
        }
        if (!StringUtils.isEmpty(subjectParentId)) {
            wrapper.eq(SUBJECT_PARENT_ID,subjectParentId);
        }
        if (!StringUtils.isEmpty(title)) {
            wrapper.like(TITLE,title);
        }
        baseMapper.selectPage(coursePage, wrapper);
        long pages = coursePage.getPages();
        long total = coursePage.getTotal();
        List<Course> records = coursePage.getRecords();
        records.forEach(record -> {
            this.getTeacherAndSubjectName(record);
        });
        Map<String, Object> map = new HashMap<>();
        map.put(TOTAL,total);
        map.put(TOTAL_PAGE,pages);
        map.put(RECORDS,records);
        return map;
    }


    @Override
    public Map<String, Object> getCourseInfoListById(Long id) {
        Course course = baseMapper.selectById(id);
        course.setViewCount(course.getViewCount()+1);
        baseMapper.updateById(course);

        CourseVo courseVo = baseMapper.selectCourseVoById(id);
        // 获取章节小节
        List<ChapterVo> chapterVoList = chapterService.getNestedTreeList(id);
        // 获取课程简介
        CourseDescription courseDescription = courseDescriptionService.getById(id);
        // 获取所属讲师信息
        Teacher teacher = teacherService.getById(courseVo.getTeacherId());

        Map<String, Object> map = new HashMap<>();

        Boolean isBuy = false;

        map.put(COURSE_VO, courseVo);
        map.put(CHAPTER_VO_LIST, chapterVoList);
        map.put(DESCRIPTION, null != courseDescription ? courseDescription.getDescription() : "");
        map.put(TEACHER, teacher);
        map.put(IS_BUY, isBuy);//是否购买
        return map;
    }

    @Override
    public List<Course> findList() {
        List<Course> list = baseMapper.selectList(null);
        list.forEach(item -> {
            this.getTeacherAndSubjectName(item);
        });
        return list;
    }

    private Course getTeacherAndSubjectName(Course record) {
        Teacher teacher = teacherService.getById(record.getTeacherId());
        if (teacher != null) {
            record.getParam().put(TEACHER_NAME,teacher.getName());
        }
        Subject subject = subjectService.getById(record.getSubjectId());
        if (subject != null) {
            record.getParam().put(SUBJECT_TITLE, subject.getTitle());
        }
        Subject subjectParent = subjectService.getById(record.getSubjectParentId());
        if (subject != null) {
            record.getParam().put(SUBJECT_PARENT_TITLE,subjectParent.getTitle());
        }
        return record;
    }
}
